<template>
  <div class="fox">
    <div class="top">
      <div class="top-left">
        1024
      </div>
      <div class="top-morden">
        <div class="morden-left">
          <span class="iconfont icon-icon_bofang"></span>
          <div>正在播放 <span>{{music_play}}</span></div>
        </div>
        <div class="morden-right">
          <span class="iconfont icon-icon-"></span>
          <div>下一首 <span>{{music_next}}</span></div>
        </div>
      </div>
    </div>
    <div class="section">
      <div>
        <el-row>
          <el-col :span="10" style="margin-left: 80px;">
            <el-table :data="tableData" border style="width: 100%" max-height="300px">
              <el-table-column prop="music_name" label="歌曲名" width="120">
              </el-table-column>
              <el-table-column prop="music_singer_name" label="歌手" width="120">
              </el-table-column>
              <el-table-column prop="music_duration" label="时长" width="120">
              </el-table-column>
              <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                  <el-button @click="addToPlayList(scope.row)" type="text" size="small">添加至列表</el-button>

                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="10" style="margin-left: 80px;">
            <el-form ref="form" :model="form" label-width="100px" max-height="400px">
              <el-form-item label="搜索">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item label="类型">
                <el-select v-model="form.region" placeholder="请选择搜索类型">
                  <el-option label="歌手名缩写" value="singer"></el-option>
                  <el-option label="歌曲名缩写" value="sang"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit">立即搜索</el-button>
                <el-button @click="undo">取消</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
    <ul class="foot" style="margin: 7.5px; padding: 7.5px;">
      <li class="nav-item">
        <span class="iconfont icon-shouye"></span>
        <div class="navItemTitle">
          <router-link to='main'>
            <div id="button1">首页</div>
          </router-link>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-fuwu"></span>
        <div class="navItemTitle">
          <div id="button2">服务</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-yuanchangbanchang"></span>
        <div class="navItemTitle">
          <div id="button3">伴唱</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-luyinshuohuashengyin"></span>
        <div class="navItemTitle">
          <div id="button4">录音</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-tiaoyin"></span>
        <div class="navItemTitle">
          <div id="button5">调音</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-zhongchang"></span>
        <div class="navItemTitle">
          <div id="button6">重唱</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-jia"></span>
        <div class="navItemTitle">
          <div id="button7"></div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-redian"></span>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-jian"></span>
        <div class="navItemTitle">
          <div id="button8"></div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-sousuo"></span>
        <div class="navItemTitle">
          <router-link to='/search'>
            <div id="button9">搜索</div>

          </router-link>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-bofangheqiege" @click="nextMusic"></span>
        <div class="navItemTitle" @click="nextMusic">
          <div id="button10" @click="nextMusic">切歌</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-fenlei"></span>
        <div class="navItemTitle">
          <router-link to='/allsinger'>
            <div id="button11">歌手</div>
          </router-link>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-yidian"></span>
        <div class="navItemTitle">
          <el-popover placement="top-end" width="400" trigger="click">
            <el-table :data="Playlist" style="width: 100%" max-height="250">
              <el-table-column prop="music_name" label="歌曲名" width="120">
              </el-table-column>
              <el-table-column prop="music_singer_name" label="歌手" width="120">
              </el-table-column>
              <el-table-column prop="music_duration" label="时长" width="120">
              </el-table-column>
              <el-table-column fixed="right" label="操作" width="130">
                <template slot-scope="scope">
                  <el-button @click.native.prevent="deleteMusicInPlayList(scope.row)" type="text" size="small">
                    移除
                  </el-button>
                  <el-button @click.native.prevent="upRow(scope.row)" type="text" size="small">
                    下一首播放
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-button slot="reference" @click="showPalyList">列表</el-button>
          </el-popover>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
	import axios from 'axios'
  export default {
    data() {
      return {

		music_play: this.GLOBAL.music_play,// 当前播放的歌名
		music_next: this.GLOBAL.music_next,// 下一首要播放的歌名
        Playlist:this.GLOBAL.playList,  // 播放列表
        tableData: [],   // 存放歌手的歌曲
		music_file_add: this.GLOBAL.music_file_add, // 当前播放歌曲的文件路径
		isPlay: this.GLOBAL.isPlay, // 当前是否有歌曲在播放
        form: {
          name: '',
          region: ''

        }
      }
    },
    methods: {
	  nextMusic(){
	  		// console.log(next_music)
	  		// 切歌
	  		var music_name = this.Playlist[0].music_name
	  		var music_singer_name = this.Playlist[0].music_singer_name
	  		var music_duration = this.Playlist[0].music_duration
	  		var row = {
	  			"music_name":music_name,
	  			"music_singer_name":music_singer_name,
	  			"music_duration":music_duration
	  		}
			this.isPlay = false
	  		this.deleteMusicInPlayList(row)

	  },
	  undo(){
		  this.form.name = ''
	  },
      onSubmit() {
		var path = ""
        if(this.form.region == "singer"){
			path = `/api/music/search/singer/${this.form.name}/`
		}else {
			path = `/api/music/search/music/${this.form.name}/`
		}
        axios.get(path)
          .then((resultData) =>{

        	  var music_data = resultData.data

        	  console.log(this.tableData)
        	  // music_data.forEach(function(music, index) {
        			// console.log(music);
        	  // });
        	  var status = resultData.data.status
        	  if(status===200){
        		  this.tableData = music_data.data
        	  } else {
				  this.$message({
				                  message: resultData.data.message,
				                  type: 'warning'
				                })
			  }
            })
          .catch()
      },


      deleteMusicInPlayList(row){
      		// 将播放列表中的歌曲删除
      		// console.log(index)
      		// console.log(row)
      		var music_name = row.music_name
      		var music_singer_name = row.music_singer_name
      		var music_duration = row.music_duration
      		axios.delete(`/api/music/playlist/del/music/${music_name}/${music_singer_name}/${music_duration}/`)
      		  .then((resultData) =>{
      		          	  var status = resultData.data.status

      		          	  if(status===202){
      						this.showPalyList();
      						this.$message({
      						                message: resultData.data.message,
      						                type: 'success'
      						              })


      		          	  } else {
					this.$message({
								    message: resultData.data.message,
								    type: 'warning'
								    })
					}
      		    })
      		  .catch()
      },
      addToPlayList(row) {
        // console.log(row);
      		// 将歌曲添加到播放列表中
      		var music_name = row.music_name
      		var music_singer_name = row.music_singer_name
      		var music_duration = row.music_duration
      		// console.log(music_name)
      		axios.post(`/api/music/playlist/order/${music_name}/${music_singer_name}/${music_duration}/`)
      		  .then((resultData) =>{
      		          	  var status = resultData.data.status

      		          	  if(status===201){
      		  				var music_data = resultData.data
      		          		this.Playlist = music_data.data
      		  				// console.log(this.Playlist)
      						// console.log("添加方法")
      						// console.log(resultData)
      						this.$message({
      						                message: resultData.data.message,
      						                type: 'success'
      						              })

      		          	  } else {
					this.$message({
								    message: resultData.data.message,
								    type: 'warning'
								    })
					}
      		    })
      		  .catch()
      },
	  play(){
	  		console.log(this.Playlist.length)
	  		if(!this.isPlay){
				console.log(this.isPlay)
	  			var music_name = this.Playlist[0].music_name
	  			var music_singer_name = this.Playlist[0].music_singer_name
	  			// var music_duration = this.Playlist[0].music_duration
	  			axios.get(`/api/music/play/${music_name}/${music_singer_name}/`)
	  			  .then((resultData) =>{
	  						  var status = resultData.data.status
	  						  if(status===200){
	  							 this.music_file_add = resultData.data.music_file_add

	  							 // window.location.href = this.music_file_add
	  							 window.open(this.music_file_add)
	  							 this.isPlay = true
	  							// this.Playlist = music_data.data
	  							// console.log(this.Playlist)


	  						  }
	  				})
	  			  .catch()
	  		}
	  },
      showPalyList(){
      	// 获取播放列表中的歌曲
      	axios.get("/api/music/playlist/show/")
      	  .then((resultData) =>{
      	          	  var status = resultData.data.status
      	          	  if(status===200){
      	  				var music_data = resultData.data
      	          		this.Playlist = music_data.data
      	  				// console.log(this.Playlist)
						if(this.Playlist.length>=1){
							this.music_play = this.Playlist[0].music_name
							if (this.Playlist.length >= 2){
								this.music_next = this.Playlist[1].music_name
							}else {
								this.music_next = ''
							}
							this.play()
						}

      	          	  }
      	    })
      	  .catch()
      },


    } ,// end mothods
	created() {
		this.showPalyList()
	}
  }
</script>

<style>
  .fox {
    height: 100%;
    width: 100%px;

    background-color: #0077AA;
  }

  .top {
    height: 100px;
    float: left;
    display: flex;
  }

  .top-left {
    padding-left: 0;
    width: 200px;
    dispaly: flex;
    font-size: 50px;
    color: white;
    margin-left: 20px;
  }

  .top-morden {
    padding-left: 200px;
    width: 500px;
    display: flex;
    font-size: 10px;
    color: white;
    align-items: center;
    justify-content: space-around;
  }


  .el-form-item_lable {
    font-size: 20px;
    color: white;
  }
.section{
  height: 350px;
  width:100%;
  padding-top: 100px;
}

  .foot {
    height: 100px;
  /*  width: 100%; */
    position: relative;
    bottom: 0;
    align-items: baseline;
    display: flex;
    justify-content: space-between;
    color: white;
    padding: 10px;
  }

  .navItemTitle {
    font-size: 10px;
    /*margin-top: 16px;*/

  }

  .a {
    text-decoration: none;
  }

  .router-link-active {
    text-decoration: none;
  }

  .nav-item {
    height: 100px;
    width: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .nav-item:hover {
    color: #990055;
  }

  .foot .nav-item .iconfont {
    font-size: 20px;
    color: white;
  }

  .el-button {
    background-color: #0077AA;
    border: 0;
    padding: 0;
    color: white;
    font-size: 10px;
  }
  .el-table .warning-row{
    background: oldlace;
  }

  .el-table .success-row{
  	  background: #f0f9eb;
  }
</style>
